package com.sniper.web.utils;

import com.sniper.mybatis.service.impl.SystemConfigService;
import com.sniper.web.listener.SpringContextUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 负责保存系统基本数据,
 *
 * @author sniper
 */


public class SystemConfigUtil {

    private static SystemConfigService configService;

    static {
        configService = SpringContextUtil.getBean(SystemConfigService.class);
    }


    private static Map<String, String> systemConfig = new HashMap<>();

    static {
        init();
    }

    /**
     * 初始化数据
     */
    public static void init() {
        if (configService != null) {
            systemConfig = configService.mapAdminConfig(true);
        } else {
            systemConfig = new HashMap<>();
        }
    }

    public static Map<String, String> getSystemConfig() {
        return systemConfig;
    }

    public static String get(String key) {
        if (systemConfig.containsKey(key)) {
            return systemConfig.get(key);
        }
        return "";
    }

    public static int getInt(String key) {
        if (systemConfig.containsKey(key)) {
            return Integer.parseInt(systemConfig.get(key));
        }
        return 0;
    }

    public static long getLong(String key) {
        if (systemConfig.containsKey(key)) {
            return Long.parseLong(systemConfig.get(key));
        }
        return 0;
    }


    public static void setSystemConfig(Map<String, String> systemConfig) {
        SystemConfigUtil.systemConfig = systemConfig;
    }

    public static void clear() {
        systemConfig.clear();
    }
}
